# Makefile for the JPEG2000 plugin

MV  := mv
SED := sed

.SUFFIXES:
.SUFFIXES: .o .c

BUILD_DIR := build

SRCS := np_entry.cpp \
	npp_gate.cpp \
	npn_gate.cpp \
	TJpeg2000Plugin.cpp

OBJS := $(addprefix $(BUILD_DIR)/, $(SRCS:.cpp=.o))

JP2K_PLUGIN := $(BUILD_DIR)/jp2k_plugin.so 

CPPFLAGS := -Inpapi-headers -Iopenjpeg
CFLAGS   := -g -Wall -O2 -DXP_UNIX=1 `pkg-config --cflags gtk+-2.0`
LDFLAGS  := -Lopenjpeg -lopenjpeg

define make-depend
  $(CC) $(CPPFLAGS) -MM $1 2> /dev/null| \
  $(SED) -e 's,\($(notdir $2)\) *:,$(dir $2)\1: ,' > $3.tmp
  $(SED) -e 's/#.*//' \
      -e 's/^[^:]*: *//' \
      -e 's/ *\\$$//' \
      -e '/^$$/ d' \
      -e 's/$$/ :/' $3.tmp >> $3.tmp
  $(MV) $3.tmp $3
endef

.PHONY: all clean

all: $(JP2K_PLUGIN)

$(JP2K_PLUGIN): ${OBJS}
	${CC} -shared -o $@ $^ ${LDFLAGS}

clean:
	-@$(RM) -rf $(BUILD_DIR) 2> /dev/null

$(BUILD_DIR)/%.o : %.cpp
	@mkdir -p $(dir $@)
	@$(call make-depend,$<,$@,$(subst .o,.d,$@))
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

ifneq "$(MAKECMDGOALS)" "clean"
-include $(dependencies)
endif
